using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Globalization;

namespace Svg.Pathing
{
    public sealed class SvgCubicCurveSegment : SvgPathSegment
    {
        private PointF _firstControlPoint;
        private PointF _secondControlPoint;
        

        public PointF FirstControlPoint
        {
            get { return this._firstControlPoint; }
            set { this._firstControlPoint = value; }
        }

        public PointF SecondControlPoint
        {
            get { return this._secondControlPoint; }
            set { this._secondControlPoint = value; }
        }

        public SvgCubicCurveSegment(PointF start, PointF firstControlPoint, PointF secondControlPoint, PointF end, bool isRelative)
        {
            this.Start = start;
            this.End = end;
            this._firstControlPoint = firstControlPoint;
            this._secondControlPoint = secondControlPoint;
            this.IsRelative = isRelative;
        }

        public override void AddToPath(System.Drawing.Drawing2D.GraphicsPath graphicsPath)
        {
            graphicsPath.AddBezier(this.Start, this.FirstControlPoint, this.SecondControlPoint, this.End);
        }

        public override string ToString()
        {
            return String.Format("C {0} {1} {2}", this.FirstControlPoint, this.SecondControlPoint, this.End);
        }

        public string RenderSegmentToSvgString(string specifier, CultureInfo culture)
        {
            if (this.IsRelative)
            {
                string format = "c" + "{0}" 
                            + Math.Round((this.FirstControlPoint.X - this.Start.X),3).ToString(specifier, culture) + "{1}"
                            + Math.Round((this.FirstControlPoint.Y - this.Start.Y),3).ToString(specifier, culture) + "{2}"
                            + Math.Round((this.SecondControlPoint.X - this.Start.X),3).ToString(specifier, culture) + "{3}"
                            + Math.Round((this.SecondControlPoint.Y - this.Start.Y),3).ToString(specifier, culture) + "{4}"
                            + Math.Round((this.End.X - this.Start.X),3).ToString(specifier, culture) + "{5}"
                            + Math.Round((this.End.Y - this.Start.Y),3).ToString(specifier, culture);
                return String.Format(format,
                    ((this.FirstControlPoint.X - this.Start.X) == 0) ? "-":String.Empty,
                    ((this.FirstControlPoint.Y - this.Start.Y) == 0) ? "-" : ((this.FirstControlPoint.Y - this.Start.Y) > 0) ? "," : String.Empty,
                    ((this.SecondControlPoint.X - this.Start.X) == 0) ? "-" : ((this.SecondControlPoint.X - this.Start.X) > 0) ? "," : String.Empty,
                    ((this.SecondControlPoint.Y - this.Start.Y) == 0) ? "-" : ((this.SecondControlPoint.Y - this.Start.Y) > 0) ? "," : String.Empty,
                    ((this.End.X - this.Start.X) == 0) ? "-" : ((this.End.X - this.Start.X) > 0) ? "," : String.Empty,
                    ((this.End.Y - this.Start.Y) == 0) ? "-" : ((this.End.Y - this.Start.Y) > 0) ? "," : String.Empty
                    );
            }
            else
            {
                return ("C" + Math.Round(this.FirstControlPoint.X,3).ToString(specifier, culture) + ","
                                + Math.Round(this.FirstControlPoint.Y,3).ToString(specifier, culture) + ","
                                + Math.Round(this.SecondControlPoint.X,3).ToString(specifier, culture) + ","
                                + Math.Round(this.SecondControlPoint.Y,3).ToString(specifier, culture) + ","
                                + Math.Round(this.End.X,3).ToString(specifier, culture) + ","
                                + Math.Round(this.End.Y,3).ToString(specifier, culture));
            }
            

            
        }
    }
}
